/**
 * 页面加载完成
 */
+(function(){ 
	var reourceVue=new Vue({
		el:"#resourceContainer",
		data:function(){
			return {
				loading:false,
				loadingTxt:$.admin.loadingTxt,
				icons:fontAwesomeIcons,
				treeHeight:pageContentHeight,
				tableHeight:pageContentHeight-55,
				treeResources:{},
				treeProps:{
					children: 'children',
			        label: 'name'
				},
				treeDefaultKeys:["root"],
				treeCurrentKey:"",
				treeKeyField:"id",
				selectTreeData:{},
				
				qryForm:{
					name:""
				},
				tableResource:[],
				
				editDiag:{
					title:"新增资源",
					icon:"fa fa-leaf",
					type:1,
					form:{
						id:"",
						name:"",
						type:"1",
						identify:"",
						url:"",
						icon:"fa fa-leaf",
						remark:"",
						snum:1
					},
					show:false,
					formLabelWidth:"100px",
					rules:{
						name:[{ required: true, message: '资源名不能为空', trigger: 'blur' }],
						type:[{ required: true, message: '资源类型不能为空', trigger: 'blur' }]
					}
				}
			}
		},
		methods:{
			treeNodeClick:function(data,node,resolve) 
			{
				this.tableResource=data.children;
				this.selectTreeData=data;
			},
			treeNodeExpand:function(data,node,resolve){
				if(data.children && data.children.length>0 && data.children[0].pid)  //初始加载 
				{
					this.loading=true;
				    var parentId=data.children[0].pid;
				    var $this=this;
				    var $data=data;
					var url=ctx+"/admin/resource/findByParent";
					$.ajax({url:url,type:"POST",dataType:"json",async:false,data:{resourceId:parentId},success:function(data){
						var data=data.data;
						if(data)
						{
							for(var i=0;i<data.length;i++)
							{
							    var item=data[i];
							    item.children=[{pid:item.id}];
							}
							$data.children=data;
						}
						$this.loading=false;
					}});
				}
			},
			updateSelectTreeData:function()
			{
				if(this.selectTreeData) //更新子节点
				{
					this.loading=true;
				    var parentId=this.selectTreeData.id;
				    var $this=this;
					var url=ctx+"/admin/resource/findByParent";
					$.ajax({url:url,type:"POST",dataType:"json",data:{resourceId:parentId},success:function(data){
						var data=data.data;
						if(data)
						{
							for(var i=0;i<data.length;i++)
							{
							    var item=data[i];
							    item.children=[{pid:item.id}];
							}
						}
						$this.loading=false;
						$this.selectTreeData.children=data;
						$this.tableResource=data;
					}});
				}
			},
			loadChildNode:function(node,resolve)
			{
				node;
			},
			loadResoureRoot:function(event)  //加载根节点数据
			{
				var $this=this;
				var url=ctx+"/admin/resource/findByParent";
				$.ajax({url:url,type:"POST",dataType:"json",data:{resourceId:"root"},success:function(data){
					
					if(data && data.success==true && data.data.length>0)
					{
						var data=data.data;
						data[0].parent.children=data;
						for(var i=0;i<data.length;i++)
						{
						    var item=data[i];
						    item.children=[{pid:item.id}];
						}
						$this.treeResources=[data[0].parent];
						$this.tableResource=data;
						$this.selectTreeData=data[0].parent;
						$this.treeCurrentKey=data[0][$this.treeKeyField];
					}
				}});
			},
			
			qry:function(event) //查询
			{
				
			},
			openAddPage:function(type,event){  //打开新增/修改页面
				this.editDiag.type=type;
				this.editDiag.show=true;
				this.editDiag.icon="fa fa-leaf";
				if(1==type)
				{
					this.editDiag.title="新增资源";
					this.editDiag.form.id="";
				}
				else if(2==type)
				{
					this.editDiag.title="修改资源";
					var url=ctx+"/admin/resource/one";
					var $this=this;
					var params={resourceId:$(event.currentTarget).attr("resourceId")}
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						var data=data.data;
						if(data)
						{
							data.type=""+data.type;
							$this.editDiag.form=data;
							
							if(data.icon) //修改时设置默认图标
							{
								$this.editDiag.icon=data.icon;
							}
						}
					}});
				}
			},
			selectIcon:function(icon)
			{
				this.editDiag.form.icon="fa "+icon;
			},
			onIconInputBlur:function(event)
			{
				if(this.editDiag.form.icon==null)
				{
					this.editDiag.icon="fa fa-leaf";
				}
				else
				{
					this.editDiag.icon=this.editDiag.form.icon;
				}
			},
			save:function(event) //保存
			{
				var $this=this;
				this.$refs["resourceDialogForm"].validate(function(valid){
					if(valid==true)
					{
						var msg="保存成功";
						if($this.editDiag.type==1)
						{
							msg="保存成功";
						}
						else
						{
							msg="修改成功";
						}
						var url=ctx+"/admin/resource/save";
						var params=$this.editDiag.form;
						params.parent=$this.selectTreeData.id;
						if(!params.icon) params.icon="fa fa-leaf";
						$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
							var data=data.data;
							$this.editDiag.show=false;
							$this.$message({
						          message: msg,
						          type: 'info'
						      });
							$this.updateSelectTreeData();
						}});
					}
				});
			},
			del:function(event)  //删除资源 
			{
				var $this=this;
				var $t=$(event.currentTarget);
				this.$confirm('确定要删除?', '提示', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(function(){
		        	var url=ctx+"/admin/resource/del";
					var params={resourceId:$t.attr("resourceId")}
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						if(data.success==true)
						{
							$this.updateSelectTreeData();
							$this.$message({
					            type: 'info',
					            message: '删除成功！'
					         });
						}
						else 
						{
							$this.$message({
					            type: 'warning',
					            message: data.msg
					         });
						}
					}});
		        }).catch(function(){
		          $this.$message({
		            type: 'info',
		            message: '已取消删除'
		          });          
		        });
			},
			
			onCloseEditDialog:function(event) //关闭弹出页面
			{
				this.clearForm();
			},
			clearForm:function() //清理新增表单数据
			{
				var $resourceDialogForm =this.$refs["resourceDialogForm"];
				if($resourceDialogForm)
				{
					$resourceDialogForm.resetFields();
				}
			},
			onUsableResource:function(useStatus,resourceId)  //启用/禁用资源
			{
				var $this=this;
				if(useStatus==1)
				{
					useStatus=0;
				}
				else if(useStatus==0)
				{
					useStatus=1;
				}
				var url=ctx+"/admin/resource/update";
				var params={id:resourceId,useStatus:useStatus};
				$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
					$this.$message({
				          message: "操作成功",
				          type: 'info'
				      });
					$this.updateSelectTreeData();
				}});
			}
		}
	});
	
	reourceVue.loadResoureRoot();
})();